package bjut.core.processor;

import bjut.core.Message.RpcResponse;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: zhuzhiming
 * @Date: 2022/5/3 16:24
 */
public class UnprocessedRequest {

    private static final ConcurrentHashMap<String, CompletableFuture<RpcResponse<Object>>> UNPROCESSED_REQUEST_MAP = new ConcurrentHashMap<>();

    public void putUnprocessedRequest(String requestId, CompletableFuture<RpcResponse<Object>> future) {
        UNPROCESSED_REQUEST_MAP.put(requestId, future);
    }

    public void complete(RpcResponse<Object> rpcResponse) {

        CompletableFuture<RpcResponse<Object>> completableFuture = UNPROCESSED_REQUEST_MAP.get(rpcResponse.getRequestId());

        if (completableFuture != null) {
            completableFuture.complete(rpcResponse);
        } else {
            throw new IllegalStateException();
        }

    }

}
